package org.syncsoft.overalt.domain.user.json;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.syncsoft.overalt.common.json.AbstractJsonConverter;
import org.syncsoft.overalt.common.json.JSonTree;
import org.syncsoft.overalt.domain.user.entity.User;

/**
 * converter to community members
 * @author tptfc
 *
 */
public class UsersJsonConverter extends AbstractJsonConverter<Collection<User>> {

	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.common.json.AbstractJsonConverter#converter(org.syncsoft.overalt.common.json.JSonTree)
	 */
	@Override
	protected Collection<User> converter(JSonTree jsonTree) {
		Collection<User> users = null;
		
		if (jsonTree != null) {
			users = extractMembers(jsonTree);
		}
		
		return users;
	}

	/**
	 * 
	 * @param jsonTree
	 * @return
	 */
	private Collection<User> extractMembers(JSonTree jsonTree) {
		Collection<User> users = new ArrayList<User>();
		List<JSonTree> memberEntities = jsonTree.getJsonTreesFronEntityName("entities");

		for (JSonTree entity : memberEntities) {
			User user = new User();
			
			JSonTree userEntity = entity.getJsonTreeFronEntityName("user");
			
			if (userEntity != null) {
				populeEntity(user, userEntity);
			}
			
			users.add(user);
		}
		return users;
	}
}
